﻿using PropertyChanged;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using VIA.Project.Domain.Services;
using VIA.SiMoJi.Domain.Vision;
using VIA.VTPlatformService;
namespace VIA.SiMoJi.Domain.Option
{
    public class VisionStationOption : INotifyPropertyChanged
    {
        private string projectFilePath;
        public string SelectedFile { get; set; }
        public int InkjetRetriveTimeout { get; set; }
        public bool IsInkjetOutLine { get; set; }
        /// <summary>
        /// 补偿值
        /// </summary>
        public Pose Compensation { get; set; } = new();
        public string ProjectFilePath
        {
            get => projectFilePath;
            set
            {
                if (SetProperty(ref projectFilePath, value))
                {
                    Bottom_Camera_FlyCaptureFlowBlock1.FilePath = value;
                    Bottom_Camera_FlyCaptureFlowBlock2.FilePath = value;
                    Bottom_Camera_FlyCaptureFlowBlock1_NotBarcode.FilePath = value;
                    Bottom_Camera_FlyCaptureFlowBlock2_NotBarcode.FilePath = value;
                    Bottom_Camera_DetectFlowBlock1.FilePath = value;
                    Bottom_Camera_DetectFlowBlock2.FilePath = value;
                    Up_Camera_FlyCaptureFlowBlock1.FilePath = value;
                    Up_Camera_FlyCaptureFlowBlock2.FilePath = value;
                    Top_Camera_CaptureFlowBlock1.FilePath = value;
                    Top_Camera_CalibrationFlowBlock.FilePath = value;
                }
            }
        }
        public int ConcurrentLevel { get; set; } = 1;
        public double CalibrationLength { get; set; }
        public double TopCameraResolution { get; set; }
        public double UpCameraResolution { get; set; }
        public double BottomCameraResolution { get; set; }

        public DuiWeiMethod DuiWeiMethod { get; set; }

        /// <summary>
        /// 下相机对位0度飞拍流程
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_FlyCaptureFlowBlock1 { get; set; } = new VTFlowBlockInfo();
        /// <summary>
        /// 下相机对位180度飞拍流程
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_FlyCaptureFlowBlock2 { get; set; } = new VTFlowBlockInfo();
        /// <summary>
        /// 下相机对位0度飞拍流程-不读码
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_FlyCaptureFlowBlock1_NotBarcode { get; set; } = new VTFlowBlockInfo();
        /// <summary>
        /// 下相机对位180度飞拍流程-不读码
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_FlyCaptureFlowBlock2_NotBarcode { get; set; } = new VTFlowBlockInfo();
        /// <summary>
        /// 下相机检测0度飞拍流程
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_DetectFlowBlock1 { get; set; } = new VTFlowBlockInfo();
        /// <summary>
        /// 下相机检测180度飞拍流程
        /// </summary>
        public VTFlowBlockInfo Bottom_Camera_DetectFlowBlock2 { get; set; } = new VTFlowBlockInfo();
        //上相机读码1
        public VTFlowBlockInfo Up_Camera_FlyCaptureFlowBlock1 { get; set; } = new VTFlowBlockInfo();
        //上相机读码2
        public VTFlowBlockInfo Up_Camera_FlyCaptureFlowBlock2 { get; set; } = new VTFlowBlockInfo();
        public VTFlowBlockInfo Top_Camera_CaptureFlowBlock1 { get; set; } = new VTFlowBlockInfo();
        public VTFlowBlockInfo Top_Camera_CalibrationFlowBlock { get; set; } = new VTFlowBlockInfo();     
        public NozzleCalibrationData[] NozzleCalibrationDatas { get; set; } = [new(), new(), new(), new(), new(), new(), new(),];
        public NozzleCalibrationData[] NozzleCalibrationDatas2 { get; set; } = [new(), new(), new(), new(), new(), new(), new(),];
        public Pose FeedTrayMarkPoint { get; set; } = new();
        public Tolerance Tolerance { get; set; } = new();
        public DuiWeiAlgorithmParam TopCameraDuiWeiParam { get; set; } = new();
        /// <summary>
        /// 吸嘴角度纠偏卡控
        /// </summary>
        public double MaxNozzleAngleOffset { get; set; }
        /// <summary>
        /// 吸嘴X纠偏卡控
        /// </summary>
        public double MaxNozzleXOffset { get; set; }
        /// <summary>
        /// 入料盘X纠偏卡控
        /// </summary>
        public double MaxFeedTrayXOffset { get; set; }
        /// <summary>
        /// 入料盘Y纠偏卡控
        /// </summary>
        public double MaxFeedTrayYOffset { get; set; }
        public event PropertyChangedEventHandler? PropertyChanged;

        private bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
            {
                return false;
            }
            field = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            return true;
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class NozzleCalibrationData
    {
        public RotationCenter RotationCenter { get; set; } = new();

        public Pose MarkPoint { get; set; } = new();
    }

    public enum DuiWeiMethod
    {
        XFirst,
        AngleFirst
    }

    [AddINotifyPropertyChangedInterface]
    public class Tolerance
    {
        /// <summary>
        /// 夹爪X纠偏卡控
        /// </summary>
        public ToleranceItem<double> OffsetXTolerance = new ToleranceItem<double>("X轴纠偏卡控", "mm", true, true);
        /// <summary>
        /// R轴角度纠偏卡控
        /// </summary>
        public ToleranceItem<double> OffsetAngleTolerance = new ToleranceItem<double>("R轴纠偏卡控", "°", true, true);
    }
}
